﻿using UnityEngine;

namespace UFrame
{
    public class MouseOperationRotation
    {
        /// <summary>
        /// 鼠标按下的之前位置
        /// </summary>
        private Vector3 previousPosition;

        /// <summary>
        /// 鼠标按下之后的滑动距离
        /// </summary>
        private Vector3 offset;

        /// <summary>
        /// 鼠标抬起后距离初始位置的位置
        /// </summary>
        private Vector3 finalOffset;

        public Transform model;

        private Vector3 startPosition;

        public bool isRoll = false;
        public float zoomSpeed = 10f;
        public float lookScale = 0f;
        public string mouseScrollWheel = "Mouse ScrollWheel";
        public bool isStop = false;

        public void SetRotationMouse360Arg(Transform m)
        {
            model = m;
        }

        public void RotationMouse360()
        {
            if (model == null)
            {
                return;
            }

            if (isStop)
            {
                return;
            }

            if (Input.GetMouseButtonDown(0)) //当0键被按下一次
            {
                startPosition = Input.mousePosition;
                previousPosition = Input.mousePosition;
            }

            if (Input.GetMouseButton(0)) // 当0键被按住持续侦测(包含down和up各一次)
            {
                offset = Input.mousePosition - previousPosition;
                previousPosition = Input.mousePosition;
                Vector3 Xoffset = new Vector3(offset.x, offset.y, 0);
                model.transform.Rotate(Vector3.Cross(Xoffset, Vector3.forward).normalized, offset.magnitude,
                    Space.World);
            }

            if (Input.GetMouseButtonUp(0)) //当0键放开一次
            {
                finalOffset = Input.mousePosition - startPosition;
                //isSlide = true;
                //angle = finalOffset.magnitude;
            }

            if (isRoll)
            {
                float scrollWheel = Input.GetAxis(mouseScrollWheel);
                if (scrollWheel < 0 || scrollWheel > 0)
                {
                    var s = model.localScale;

                    float f = scrollWheel * zoomSpeed;
                    if (f < 0 && s.x <= 1)
                    {
                        f = -0.05f;
                    }

                    s = VectorTool.Add(s, f);
                    if (s.x < lookScale)
                    {
                        return;
                    }

                    model.localScale = s;
                }
            }
        }


    }
}